/* gcc -Wall OFS_1.c -o ofs -lm  TO COMPILE*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>

/*******************************************************************/
/*****************Global Variables Declaration *********************/

char	filename[100];
int	inpNode,outNode,seed1;
long	iter=0,cntTrnData=0,STEP=0;
double	**wih,**pwih,**cwih,regn_factorK;
double	**who,**pwho,**cwho;
double	***wll,***pwll,***cwll;

double	**trnData,**err,**doutTrn,**aoutput1;
double	*beta,*pbeta,**delta,*delta_inp,*outdelta,*delbeta;
double 	*attenFun,**netValHid,**hidActiv,*netValOup,*outActiv;
double	n,mu;
int 	layer,*node_in_layer;
float D;
/*******************************************************************/
/***************** Functions Declaration ***************************/

void	training();
void 	initialize();
void 	memory_free();
void 	Free(double ** , int );
void 	weight_initialization();
void 	writefile();
double	sigmoid(double );
double 	**memory2double(int ,int ,char * );
double compute_regn(double *);
double compute_regnsqr(double *);
double compute_regnquad(double *);
/*******************************************************************/

int main()
{
	int i=0;
	FILE *fpr;
	FILE *fpnrp;

	
	printf("\n Enter Regularization...");
	scanf("%lg",&regn_factorK);
	printf("\nRegularization factor...%lg",regn_factorK);
	
	printf("\n Enter learning parameter Eta:...");
	scanf("%lg",&n);
	printf("\nEta...%lg",n);
	
	printf("\n Enter learning parameter mu:...");
	scanf("%lg",&mu);
	printf("\nmu...%lg",mu);
	
	printf("\n Enter D value:...");
	scanf("%f",&D);
	printf("\nmu...%f",D);
	
	
	initialize();
	
	fpnrp=fopen("GateOpeningFile.xls","w");
	fclose(fpnrp);
	fpr=fopen("statusfileR.txt","w");
	fprintf(fpr,"The neular net architecture:\n");
	
	fprintf(fpr,"%d\t",inpNode) ;
	for(i=0;i<layer;i++)
		fprintf(fpr,"%d\t",node_in_layer[i]) ;
	fprintf(fpr,"%d\n",outNode) ;	
	fprintf(fpr,"Initial parameters:\n");	
	fprintf(fpr,"Weight learning parameter n:%f\tGate  parameter n:%f\n",n,mu) ;
	fprintf(fpr,"Regularization factor:%f\t seed for rand initialization %d \n",regn_factorK,seed1);
	fprintf(fpr,"Scaling down factor (D) for regularization penalty  :: %f \n \n",D);
	fprintf(fpr,"epoc  missclassn  SSE(P+OE)  regn_penalty(P)     o/p_err(OE)        eta            Gate openings...\n");
		
	fclose(fpr);
	
	
	training();	
	
	writefile();
	
	
	
	memory_free();
	
	return(0);	
}
/*******************************************************************/

void 	initialize()
{
	long 	i,j;
	FILE 	*fp;
	int	ch;
	
	printf("\n Enter the no of input nodes...");
	scanf("%d",&inpNode);
	
	printf("\nEnter the no of Hidden layer");
	scanf("%d",&layer);
	
	node_in_layer=(int*)calloc(layer,sizeof(int));
	
	for(i=0;i<layer;i++)
	{
		printf("\nEnter the no of node for layer %d....",i+1);
		scanf("%d",&node_in_layer[i]);
		
	}
		
	printf("\n Enter the no of output nodes...");
	scanf("%d",&outNode);
	
	printf("\n Enter the no of iterations...");
	scanf("%d",&iter);
	
	printf("\n Enter the name of the input file...");
	scanf("%s",filename);
	
	printf("\n Enter the STEP size...");
	scanf("%d",&STEP);	
	
	fp = fopen(filename,"r");
	while(!feof(fp))
	{
		if((ch=fgetc(fp))=='\n')
			cntTrnData++;			
	}
	fclose(fp);
	
	trnData = memory2double(cntTrnData,inpNode+outNode,"trnData");
	
	/*********READING THE TRAINING FILE**************/
	
	fp = fopen(filename,"r");
	
	for(i = 0; i < cntTrnData; i++)
		for(j = 0; j <(inpNode+outNode); j++)
			fscanf(fp,"%lf",&trnData[i][j]);	
		
	fclose(fp);
	
	/***** INTIALIZING THE WEGHT OF THE NN ***************/			
	weight_initialization();
		
	return;	
}
/*******************************************************************/

void weight_initialization()
{
	int l,seed;
	long 	i,j;
//	time_t  t;
	double	d;
	
	wih = memory2double(node_in_layer[0],inpNode,"wih");	
	cwih = memory2double(node_in_layer[0],inpNode,"cwih");
	pwih = memory2double(node_in_layer[0],inpNode,"pwih");
	
	who = memory2double(outNode,node_in_layer[layer-1],"who");
	cwho = memory2double(outNode,node_in_layer[layer-1],"cwho");
	pwho = memory2double(outNode,node_in_layer[layer-1],"pwho");
	
	wll=(double***)calloc(layer-1,sizeof(int**));
	cwll=(double***)calloc(layer-1,sizeof(int**));
	pwll=(double***)calloc(layer-1,sizeof(int**));

	for(i=0;i<layer-1;i++)
	{
		wll[i]=	memory2double(node_in_layer[i+1],node_in_layer[i],"wll");
		cwll[i]=memory2double(node_in_layer[i+1],node_in_layer[i],"cwll");
		pwll[i]=memory2double(node_in_layer[i+1],node_in_layer[i],"pwll");
	}
	
	
	/****** MEMORY ALLOCATION TO STORE ATTENUATION VALUES ******/
	
	attenFun = (double*)calloc(inpNode,sizeof(double));
	beta = (double*)calloc(inpNode,sizeof(double));
	pbeta = (double*)calloc(inpNode,sizeof(double));	
	
	delta=(double**)calloc(layer,sizeof(double*));
	for(i=0;i<layer;i++)
		delta[i] = (double*)calloc(node_in_layer[i],sizeof(double));
	
	delta_inp =(double*)calloc(inpNode,sizeof(double));
	outdelta=(double*)calloc(outNode,sizeof(double));
	delbeta	=(double*)calloc(inpNode,sizeof(double));
	
	//srand((unsigned)time(&t));
	//printf("\nthe value of seed  1 is %u",time(&t));
	        printf("Enter the value of seed:");
	        scanf("%d",&seed);
		srand((unsigned)seed);
	seed1=seed;
	for(i = 0;i <node_in_layer[0]; i++)
	{
		for(j = 0 ; j <inpNode ; j++)
		{			
			d=(double)rand()/(RAND_MAX);
			wih[i][j]=d-0.5;
			pwih[i][j] = wih[i][j];			
		}		
	}
	
		
	//srand((unsigned)time(&t));
	//printf("\nthe value of seed  1 is %u",time(&t));
	
	//srand((unsigned)seed);
	for(l=0;l<layer-1;l++)
	{
		for(i = 0;i <node_in_layer[l+1]; i++)
		{
			for(j = 0 ; j <node_in_layer[l] ; j++)
			{			
				d=(double)rand()/(RAND_MAX);
				wll[l][i][j]=d-0.5;
				pwll[l][i][j] = wll[l][i][j];			
			}		
		}
	}
	
	
	//srand((unsigned)time(&t));
	//printf("\nthe value of seed  1 is %u",time(&t));
	
	//srand((unsigned)seed);
	for(i = 0; i < outNode; i++)
	{
		for(j = 0; j < node_in_layer[layer-1]; j++)
		{			
			d=(double)rand()/(RAND_MAX);
			who[i][j]=d-0.5;			
			pwho[i][j] = who[i][j];			
		}		
	}	
	
	for( i = 0; i < inpNode; i++)
	{
		beta[i] = -5.0;
		pbeta[i] = -5.0;
		/* pbeta[0]=pbeta[1]=beta[0]=beta[1]=5.0; pbeta[2]=pbeta[3]=beta[2]=beta[3]=-5;*/
		
		attenFun[i] = sigmoid(beta[i]);
	}
	
		
	
	return;			
}
/*******************************************************************/

double **memory2double(int a,int b,char *s)
{
	double **temp;
	int i;
	
	temp = (double**)calloc(a,sizeof(double*));
	
	if(temp == NULL)
	{
		printf("\n Error %s\n",s);
		exit(0);
	}
	for(i = 0; i < a; i++)
	{
		temp[i] = (double*)calloc(b,sizeof(double));
		if(temp[i] == NULL)
		{
			printf("\n Error %s[%d]\n",s,i);
			exit(0);
		}
	}
	
	return(temp);
}
/*******************************************************************/

double sigmoid(double x)
{
	double sig;
	double d;
	d=exp(-x);
	d=d+1;
	sig = (double)(1/d);
	return(sig);
}
/*******************************************************************/

void training()
{	
	int l;
	FILE	*fp,*fpr,*fpnrp;
	long 	i,j,k,maxacol,maxdcol;	
	char	statusfile[100];
	char	attenuation_file[100];	
	double 	*iv,temp,maxa,maxd,regn=0.0,regn_beta=0.0;
	long	Misclsf=0,epoc=0;
	double	prevsse=0,sse=0;
	
	/**********************************************************/
	strcpy(statusfile,filename);
	strcat(statusfile,"_sfs.txt");
	
	strcpy(attenuation_file,filename);
	strcat(attenuation_file,"_atn.txt");
	/************************************************************/
	fp = fopen(statusfile,"w");
	fclose(fp);
	
	fp = fopen(attenuation_file,"w");
	fclose(fp);

	printf(" No of patterns in the training file : %d\n",cntTrnData);
	
	/***DYNAMIC MEMORY ALLOCATION FOR THE DESIRED OUTPUT ARRAY**/
	
	doutTrn = memory2double(cntTrnData,outNode,"doutTrn");
	
	aoutput1= memory2double(cntTrnData,outNode,"aoutput1");
	err	= memory2double(cntTrnData,outNode,"err");
	
	/***DYNAMIC MEMORY ALLOCATION FOR THE ACTIVATION VALUES*****/
	netValHid = (double**)calloc(layer,sizeof(double));
	hidActiv = (double**)calloc(layer,sizeof(double*));	
	
	for(l=0;l<layer;l++)
	{
		netValHid[l]=(double*)calloc(node_in_layer[l],sizeof(double));
		hidActiv[l]=(double*)calloc(node_in_layer[l],sizeof(double));
	}
	
	netValOup = (double*)calloc(outNode,sizeof(double));
	outActiv  = (double*)calloc(outNode,sizeof(double));
	
	for(i = 0; i < cntTrnData; i++)
		for(j=0 ; j<outNode; j++)
			doutTrn[i][j] = trnData[i][inpNode+j];			
			
	iv=(double *)calloc(inpNode,sizeof(double));
	
	epoc = 0;
   	while(epoc<iter )
   	{	
		for(k=0;k<cntTrnData;k++)
		{
			for(i=0;i<inpNode;i++)
				iv[i]=trnData[k][i]*attenFun[i];
	
			/***  HIDDEN ACTIVATION CALCULATION ********/
						
			for(i=0;i<node_in_layer[0];i++)
			{
				netValHid[0][i]=0;
				for(j=0;j<inpNode;j++)
					netValHid[0][i]+=iv[j]*wih[i][j];
				hidActiv[0][i]=sigmoid(netValHid[0][i]);
			}
			
			for(l=1;l<layer;l++)
			{
				for(i=0;i<node_in_layer[l];i++)
				{	
					netValHid[l][i]=0;
					for(j=0;j<node_in_layer[l-1];j++)
						netValHid[l][i]+=hidActiv[l-1][j]*wll[l-1][i][j];
					hidActiv[l][i]=sigmoid(netValHid[l][i]);	
				}
				
			}
			
	  		/*******   OUTPUT CALCULATION  *************/
	
			for(i=0;i<outNode;i++)
			{	
				netValOup[i]=0;
				for(j=0;j<node_in_layer[layer-1];j++)
					netValOup[i]+=hidActiv[layer-1][j]*who[i][j];
				outActiv[i]=sigmoid(netValOup[i]);
			}		
	
			/****** OUTPUT ERROR CALCULATION   *********/
			
			
			for(i=0;i<outNode;i++)
			{
				
				err[k][i]=(doutTrn[k][i]-outActiv[i]);
				
			}	
			
			
				
			/*******  OUTPUT LAYER DELTA  **************/
		
			for(i=0;i<outNode;i++)
				outdelta[i]=err[k][i]*outActiv[i]*(1-outActiv[i]);
			
			/******  HIDDEN TO OUTPUT DELTA WEIGHT *****/
	
			for(i=0;i<outNode;i++)
				for(j=0;j<node_in_layer[layer-1];j++)
		  		 	cwho[i][j]=n*outdelta[i]*hidActiv[layer-1][j];/*******layer-1*/

			/*****  HIDDEN LAYER DELTA CALCULATION *******/
			
			
			for(i=0;i<node_in_layer[layer-1];i++)/*******layer-1*/
			{
				delta[layer-1][i]=0.0;/*******layer-1*/
				for(j=0;j<outNode;j++)
					delta[layer-1][i]+=outdelta[j]*who[j][i];/*******layer-1*/
				delta[layer-1][i]=delta[layer-1][i]*hidActiv[layer-1][i]*(1-hidActiv[layer-1][i]);/*******layer-1*/
				
			}	
			
						
			for(l=layer-2;l>=0;l--)
			{
				for(i=0;i<node_in_layer[l];i++)
				{
					delta[l][i]=0.0;
					for(j=0;j<node_in_layer[l+1];j++)
						delta[l][i]+=delta[l+1][j]*wll[l][j][i];
						
					delta[l][i]=delta[l][i]*hidActiv[l][i]*(1-hidActiv[l][i]);
				
				}
			}
			
						
			/***************************WLL UPDATION*****************************/
			for(l=0;l<layer-1;l++)
			{
				for(i=0;i<node_in_layer[l+1];i++)
				{
					for(j=0;j<node_in_layer[l];j++)

						cwll[l][i][j]=n*delta[l+1][i]*hidActiv[l][j];
				}
			}
			/********************************************************************/
	
			/******  INPUT TO FIRST HIDDEN DELTA WEIGHT ******/
	
			for(i=0;i<node_in_layer[0];i++)
				for(j=0;j<inpNode;j++)
					cwih[i][j]=n*delta[0][i]*iv[j];
						
			/****  ACTUAL INPUT TO ATTENUATE DELTA *****/
	
			for(j=0;j<inpNode;j++)
				delta_inp[j]=0.0;
			
			for(i=0;i<node_in_layer[0];i++)
				for(j=0;j<inpNode;j++)
					delta_inp[j]+=wih[i][j]*delta[0][i];
				
		
			for(j=0;j<inpNode;j++)
			{	
				temp = sigmoid(beta[j])*(1-sigmoid(beta[j]));
				delbeta[j]=mu*trnData[k][j]*temp*delta_inp[j];
			}
			
			/*********     UPDATION OF WHO     *********/
	
			for(i=0;i<outNode;i++)
				for(j=0;j<node_in_layer[layer-1];j++)
					who[i][j]=who[i][j]+cwho[i][j];
				
			/**********   UPDATION OF WLL      *********/
			
			for(l=0;l<layer-1;l++)
				for(i=0;i<node_in_layer[l+1];i++)
					for(j=0;j<node_in_layer[l];j++)
						wll[l][i][j]=wll[l][i][j]+cwll[l][i][j];

			/**********   UPDATION OF WIH      *********/
	
			for(i=0;i<node_in_layer[0];i++)
				for(j=0;j<inpNode;j++)
					wih[i][j]=wih[i][j]+cwih[i][j];							
	
			/******** UPDATION OF ATTENUATORS  *********/	
			/* The addition of Regularization factor is added to the error termas penalty,whose effet is*/
			/* added to the modification of GATE opening factor beta[i]  */
			//printf("\nvalue of K is %f",regn_factorK);
			/*if(regn_factorK > 0.0) {
				//printf("\nregn_beta satishfied");
				regn_beta=1.0; 
			}*/
			for(i=0;i<inpNode;i++)
			{
				beta[i]=beta[i]+ delbeta[i] - mu * exp(-1*(D-beta[i])) * regn_factorK;
				
				if(abs(mu * exp(-1.0*(D-beta[i])) * regn_factorK)>3)
				{    printf("\n****regn  value for %d is %f  with beta %f",i,mu * exp(-1*(D-beta[i])) * regn_factorK,beta[i]);
				exit(1);
				}
				attenFun[i]=sigmoid(beta[i]);
				/*if(abs(beta[i])>6.0)
				 printf("\n***beta value for %d is %f with addbeta %f ",i,beta[i],mu * exp(-1*(D-beta[i]) ) * regn_factorK);
				 */
				 
			}
			regn_beta=0;
		}
		
		/****************** Section Copied *****************/
		/***************************************************/
		
		Misclsf = 0;
		sse = 0;								
		regn=compute_regn(beta);
		/*if(regn_factorK > 0.0) 
			{	
				regn_beta=1.0; 
			}*/
		for(k = 0; k < cntTrnData; k++)
		{
			for(i=0;i<node_in_layer[0];i++)
			{
				netValHid[0][i]=0;
				for(j=0;j<inpNode;j++)
					netValHid[0][i]+=(trnData[k][j] * attenFun[j])*wih[i][j];
				hidActiv[0][i]=sigmoid(netValHid[0][i]);
			}
			
			for(l=1;l<layer;l++)
			{
				for(i=0;i<node_in_layer[l];i++)
				{	
					netValHid[l][i]=0;
					for(j=0;j<node_in_layer[l-1];j++)
						netValHid[l][i]+=hidActiv[l-1][j]*wll[l-1][i][j];
					hidActiv[l][i]=sigmoid(netValHid[l][i]);	
				}
				
			}
		
	  		/*******   OUTPUT CALCULATION  *************/
			
			
			for(i=0;i<outNode;i++)
			{	
				netValOup[i]=0;
				aoutput1[k][i] = 0;
				for(j=0;j<node_in_layer[layer-1];j++)
					netValOup[i]+=hidActiv[layer-1][j]*who[i][j];
				
				aoutput1[k][i] = sigmoid(netValOup[i]);
				err[k][i] = doutTrn[k][i] - aoutput1[k][i];
				sse+= err[k][i] * err[k][i] ; 
			}			
			sse+= regn ;
			
			
			if(epoc == 0 )
			{
				
				for(i=0 ; i<outNode ;i++)
					prevsse+= err[k][i] * err[k][i];
				prevsse+= regn;	
			}
				
			maxacol = -1;
			maxdcol = -1;
			maxa = -1;
			maxd = -1;
			
			for(i=0 ; i<(outNode) ;i++)
			{				
				if(maxa<aoutput1[k][i])
				{
					maxa=aoutput1[k][i];
					maxacol=i;					
				}

				if(maxd<doutTrn[k][i])
				{
					maxd=doutTrn[k][i];
					maxdcol=i;					
				}				
			}	
			
			if(maxdcol!=maxacol)
				Misclsf++;							
		}

		regn_beta=0;
		if(sse > prevsse)
		{			
			for(i = 0; i < node_in_layer[0]; i++)
				for(j = 0;j < inpNode; j++)
					wih[i][j] = pwih[i][j];	
			
			for(l=0;l<layer-1;l++)
				for(i=0;i<node_in_layer[l+1];i++)
					for(j=0;j<node_in_layer[l];j++)
						wll[l][i][j]=pwll[l][i][j];
			
			
							
			for(i = 0 ;i < outNode; i++)
				for(j = 0; j < node_in_layer[layer-1]; j++)
					who[i][j] = pwho[i][j];
				
			for(i=0; i<inpNode; i++)
			{
				beta[i] = pbeta[i];
				attenFun[i] = sigmoid(beta[i]);
			}			
						
			n=0.95*n;
			mu=0.95*mu; 
			
			/*epoc--;*/
			
			sse=prevsse;			
		} 
		else if(sse <= prevsse)
		{ 
			prevsse = sse;
			for(i = 0; i < outNode; i++)
				for(j = 0; j < node_in_layer[layer-1]; j++)
					pwho[i][j] = who[i][j];
		  	
			for(l=0;l<layer-1;l++)
				for(i=0;i<node_in_layer[l+1];i++)
					for(j=0;j<node_in_layer[l];j++)
						pwll[l][i][j]=wll[l][i][j];
			
				
			for(i = 0;i < node_in_layer[0]; i++)
				for(j = 0; j < inpNode; j++)
					pwih[i][j] = wih[i][j];
				
			for(i=0 ; i<inpNode ; i++)
				pbeta[i] = beta[i];	
		} 
				
		if((epoc % STEP) == 0)
		{
			printf("\n Epoch= %d",epoc);
			printf("\n sse = %lf",sse);
			printf("\n Misclas = %d ",Misclsf);
			printf("\n Learning parameter = %lf",n);
			printf("\n learning parameter for attenfun = %lf\n",mu);
			for(i = 0; i < inpNode; i++)
			{
				printf(" af[%d] = %lf\n",i,attenFun[i]);
				//printf("beta value for %d is %f\n",i,beta[i]);
			}
			fp=fopen(statusfile,"a");
		
			/* addition by nrp ***/
			fpnrp=fopen("GateOpeningFile.xls","a");
            fprintf(fpnrp,"Iteration Number %d \n", epoc) ;
			/* addition by nrp over ***/

			fpr=fopen("statusfileR.txt","a");
			fprintf(fp,"%d \t %d \t %lf \t %lf \t %lf \t %lf \n", epoc,Misclsf,sse,cntTrnData*regn, sse-cntTrnData*regn,n ) ;
			fprintf(fpr,"%d \t %d \t %lf \t %lf \t %lf \t %lf \n", epoc,Misclsf,sse,cntTrnData*regn, sse-cntTrnData*regn,n ) ;
			
			//fclose(fp);
			
			//fp=fopen(attenuation_file,"a");
			for(i=0; i<inpNode-1; i++)
				{
					fprintf(fp,  "%d\t%lf  \n ",i,attenFun[i]);
					fprintf(fpr,"%d\t%lf  \n ",i,attenFun[i]);
						/* addition by nrp ***/
					fprintf(fpnrp,"%d\t%lf \n ",i,attenFun[i]);
						/* addition by nrp over ***/
				}	
			fprintf(fp,"%d\t%lf\n",i,attenFun[i]);
			fprintf(fpr,"%d\t%lf\n",i,attenFun[i]);
			/* addition by nrp ***/
			fprintf(fpnrp,"%d\t%lf  \n ",i,attenFun[i]);
			fclose(fpnrp);
			/* addition by nrp over ***/
			fclose(fp);			
			fclose(fpr);
		}		
	
		epoc++;
	}
	
	free(iv);	
	return;	
}
/*******************************************************************/

void writefile()
{
	FILE 	*fp1,*fp2;
	char 	filename1[200],filename2[200];
	int 	i,k,k1;	
	double 	maxa,maxd;
	int 	maxacol,maxdcol; 
	
	strcpy(filename1,filename);
	strcat(filename1,".pre.txt");
	
	strcpy(filename2,filename);
	strcat(filename2,".act.txt");
	fp1=fopen(filename1,"w");	
	fp2=fopen(filename2,"w");
	
	for(k=0;k<cntTrnData;k++)
	{
		maxa=-1,maxd=-1;
		maxacol=-1,maxdcol=-1; 
		
		for(i=0 ; i<(outNode) ;i++)
		{			
			if(maxa<aoutput1[k][i])
			{
				maxa=aoutput1[k][i];
				maxacol=i;
			}

			if(maxd<doutTrn[k][i])
			{
				maxd=doutTrn[k][i];
				maxdcol=i;
			}
		}	

		if(maxacol!=maxdcol)
		{
			for(k1=0;k1<outNode;k1++)
				fprintf(fp1,"%lf\t",aoutput1[k][k1]);
			fprintf(fp1,"\n");
				
			for(k1=0;k1<outNode;k1++)
				fprintf(fp2,"%lf\t",doutTrn[k][k1]);
			fprintf(fp2,"\n");
		}		
	}	
	
	fclose(fp1);
	fclose(fp2);
	
	return;
}
/*******************************************************************/

void memory_free()
{
	int i,j;
	for(i=0;i<layer-1;i++)
	{
		for(j=0;j<node_in_layer[i+1];j++)
		{
			free(wll[i][j]);
			free(pwll[i][j]);
			free(cwll[i][j]);
		}
	}
	for(i=0;i<layer-1;i++)
	{	
		free(wll[i]);
		free(pwll[i]);
		free(cwll[i]);	
	}
	free(wll);
	free(pwll);
	free(cwll);
		
	
	Free(wih,node_in_layer[layer-1]);
	Free(cwih,node_in_layer[layer-1]);
	Free(pwih,node_in_layer[layer-1]);
	Free(who,outNode);
	Free(cwho,outNode);
	Free(pwho,outNode);
	
	free(attenFun);
	free(beta);
	free(pbeta);	
	free(delta_inp);
	free(outdelta);
	free(delbeta);
	
	Free(delta,layer);
	Free(trnData,cntTrnData);
	Free(doutTrn,cntTrnData);

	Free(aoutput1,cntTrnData);
	Free(err,cntTrnData);
	Free(netValHid,layer);
	Free(hidActiv,layer);
	free(netValOup);
	free(outActiv);
	free(node_in_layer);

	return;	
}	
/*******************************************************************/

void Free(double **node, int a)
{
	int i;
	
	for(i=0;i<a;i++)
		free(node[i]);
	
	free(node);
	return;
}
/*******************************************************************/
double compute_regn(double *beta)
{
	int j;
	double sum=0.0;
	for(j=0;j<inpNode;j++)
	{
	sum+=exp(-1*(D-beta[j]));
	}
	sum=regn_factorK*(sum);
return sum; 
//return ( 0.8*exp(-1*(D-betai)) );
}

double compute_regnsqr(double *beta)
{
	int i;
	double sum=0.0;
	for(i=0;i<inpNode;i++)
	{
	sum+=pow((beta[i]+D),2)/144;
	}
	sum=regn_factorK*(sum);
return sum; 
}

double compute_regnquad(double *beta)
{
	int i;
	double sum=0.0;
	for(i=0;i<inpNode;i++)
	{
	sum+=pow((beta[i]+D),4 )/(144 * 144);
	}
	sum=regn_factorK*(sum);
return sum; 
}
/*******************************************************************/

